﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using System.Collections.Generic;
using ClassLibrary.DAO;

public partial class Yumcha_UcControl_ucConfirmOrderAndBooking : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if ((Session[Global.SESSION_BOOK] == null) || (Session[Global.SESSION_TOTAL] == null) || (Session[Global.SESSION_CART] == null))
            {
                Response.Redirect("~/Yumcha/Shopping_Cart.aspx");
            }
            else
            {
                List<TblBookingDetail> arrItems = new List<TblBookingDetail>();
                if (Session[Global.SESSION_CART] != null)
                    arrItems = (List<TblBookingDetail>)Session[Global.SESSION_CART];
                grdShoppingCart.DataSource = arrItems;
                grdShoppingCart.DataBind();
                TblBooking booking;
                if (Session[Global.SESSION_BOOK] == null)
                    booking = new TblBooking();
                else
                    booking = (TblBooking)Session[Global.SESSION_BOOK];

                Session[Global.SESSION_BOOK] = booking;

                TblCustomers customer;
                if (Session[Global.SESSION_LOGGED_USER] != null)
                    customer = (TblCustomers)Session[Global.SESSION_LOGGED_USER];
                else
                    customer = new TblCustomers();

                lb_name.Text = customer.FName + " " + customer.LName;
                lb_phone.Text = customer.MobilePhone;
              
                lb_email.Text = ((TblCustomers)Session[Global.SESSION_LOGGED_USER]).EmailAdress;
                lb_OrderDate.Text = Convert.ToDateTime(booking.DateEvent).ToString();

                lblTotal.Text = (float.Parse(Session[Global.SESSION_TOTAL].ToString())).ToString("c");
            }

        }
    }
    
    protected void btnThanhToan_Click(object sender, EventArgs e)
    {
        bool flag = false;
        if ((Session[Global.SESSION_TOTAL] == null) || (Session[Global.SESSION_BOOK] == null) || (Session[Global.SESSION_CART] == null))
        {
            Response.Redirect("~/Yumcha/AlaCarteMenu.aspx");
        }
        else
        {
            try
            {
                TblBookingDAO bookingDAO = new TblBookingDAO(Global.CONNECTION_STRING);
                TblBooking booking;
                if (Session[Global.SESSION_BOOK] == null)
                {
                    booking = new TblBooking();
                }
                else
                {
                    booking = (TblBooking)Session[Global.SESSION_BOOK];
                }

                TblCustomers userAccount = (TblCustomers)Session[Global.SESSION_LOGGED_USER];
                booking.Customer = userAccount;
                booking.CusID = userAccount.CusID;

                booking.DateBook = DateTime.Now;
                booking.DateEvent = Convert.ToDateTime(lb_OrderDate.Text);
                booking.Status = 1;
                // order.Total = float.Parse(Session[Global.SESSION_TOTAL].ToString());

                booking.TotalPrice = Decimal.Parse(Session[Global.SESSION_TOTAL].ToString());

                List<TblBookingDetail> arrItems = (List<TblBookingDetail>)Session[Global.SESSION_CART];
                int id;

                bookingDAO.Insert(booking, arrItems, out id);

                Session[Global.SESSION_CART] = null;
                Session[Global.SESSION_TOTAL] = null;

                flag = true;
            }
            catch (Exception ex)
            {
                Session[Global.SESSION_ERROR] = ex;
                Response.Redirect(Global.ERROR_PAGE, true);
            }
            if (flag)
            {
                ScriptManager.RegisterStartupScript(btnThanhToan, btnThanhToan.GetType(), "ScriptLoginSuccess", "UpdateParentsLogin();", true);
            }
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Yumcha/OrderDetails.aspx");
    }
}
